package com.block.common.service.impl;

import com.block.common.service.IAppEncryptService;
import com.block.db.entity.AppEncrypt;
import com.block.db.mapper.AppEncryptMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * app加密配置表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-05
 */
@Service
public class AppEncryptServiceImpl extends ServiceImpl<AppEncryptMapper, AppEncrypt> implements IAppEncryptService {

    @Resource
    private RedisTemplate<String,AppEncrypt> redisAppEncryptTemplate;

    private  static  final  String APPENCRY_KEY_PREFIX="APPENCRY:APPID:";
    private  static  final  Long APPENCRY_KEY_TIME_OUT=4L;

    @Override
    public AppEncrypt getByAppId(String appId) {
        AppEncrypt appEncrypt= redisAppEncryptTemplate.opsForValue().get(APPENCRY_KEY_PREFIX+appId);
        if(appEncrypt==null)
        {
            System.out.println("未从redis读取信息");
            appEncrypt= this.baseMapper.getByAppId(appId);
            if(appEncrypt!=null) {
                redisAppEncryptTemplate.opsForValue().set(APPENCRY_KEY_PREFIX + appId, appEncrypt, APPENCRY_KEY_TIME_OUT, TimeUnit.HOURS);
            }
        }
        return appEncrypt;
    }
}
